package it.uniroma2.art.coda.projectionrule.graph;

import java.util.Map;

import it.uniroma2.art.coda.projectionrule.PlaceholderStruct;
import it.uniroma2.art.coda.projectionrule.ProjectionRule;

/**
 * This class is the single element for a placeholder
 * 
 * @author Andrea Turbati
 */
public class GraphSingleElemPlaceholder extends GraphSingleElement {
	private String placeholderId;
	private boolean hasExternalReference;
	private String otherProjRuleId; // the id of the external projection rule, if it exists
	private PlaceholderStruct placeholder; // the internal or the external placeholder used by this graph
	private boolean dynamicExternalReference;
	
	/**
	 * @param placeholderId
	 *            the placeholder id
	 * @param otherProjRuleId
	 *            the of the another projection rule where the placeholder is defined
	 * @param prRule
	 *            the projection rule where this element belongs to
	 */
	public GraphSingleElemPlaceholder(String placeholderId, String otherProjRuleId, ProjectionRule prRule) {
		super(prRule);
		this.placeholderId = placeholderId;
		this.hasExternalReference = true;
		this.otherProjRuleId = otherProjRuleId;
		placeholder = null;
	}
	
	/**
	 * @param placeholderId
	 *            the placeholder id
	 * @param otherProjRuleId
	 *            the of the another projection rule where the placeholder is defined
	 * @param isDynRef
	 * 			  true if it is a dynamic reference, false otherwise
	 * @param prRule
	 *            the projection rule where this element belongs to
	 */
	public GraphSingleElemPlaceholder(String placeholderId, String otherProjRuleId, boolean isDynRef, ProjectionRule prRule) {
		super(prRule);
		this.placeholderId = placeholderId;
		this.hasExternalReference = true;
		this.otherProjRuleId = otherProjRuleId;
		this.dynamicExternalReference = isDynRef;
		placeholder = null;
	}

	/**
	 * @param placeholderId
	 *            the placeholder id
	 * @param prRule
	 *            the projection rule where this element belongs to
	 */
	public GraphSingleElemPlaceholder(String placeholderId, ProjectionRule prRule) {
		super(prRule);
		this.placeholderId = placeholderId;
		this.hasExternalReference = false;
		this.otherProjRuleId = null;
		Map<String, PlaceholderStruct> placeholderMap = prRule.getPlaceholderMap();
		placeholder = placeholderMap.get(placeholderId);
		if (placeholder == null){
			setIssue(true);
		}
	}

	/**
	 * Get the id of the placeholder
	 * 
	 * @return the id of the placeholder
	 */
	public String getPlaceholderId() {
		return placeholderId;
	}

	/**
	 * Check if this element has an external reference
	 * 
	 * @return true if the palceholder is defined in another projection rule, false otehrwise
	 */
	public boolean hasExternalReference() {
		return hasExternalReference;
	}

	/**
	 * Get the other projection rule id
	 * 
	 * @return the other projection rule id or null if the placeholder is defined in the same projection rule
	 */
	public String getOtherProjRuleId() {
		return otherProjRuleId;
	}

	/**
	 * Get the plaeholder
	 * 
	 * @return the palceholder
	 */
	public PlaceholderStruct getPlaceholder() {
		return placeholder;
	}

	/**
	 * Set the placeholder
	 * 
	 * @param placeholder
	 *            the placeholder contained in this element
	 */
	public void setPlaceholder(PlaceholderStruct placeholder) {
		this.placeholder = placeholder;
	}
	
	/**
	 * Set true if it has a dynamic reference, false otherwise
	 * 
	 * @param dynamicExtRef true if it has a dynamic reference, false otherwise
	 */
	public void setHasDynamicExternalReference(boolean dynamicExtRef){
		dynamicExternalReference = dynamicExtRef;
	}
	
	/**
	 * Check if it has a dynamic reference
	 * 
	 * @return true if it has a dynamic reference, false otherwise
	 */
	public boolean hasDynRef(){
		return dynamicExternalReference;
	}
}
